<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>兑换列表</el-breadcrumb-item>
    </el-breadcrumb>


    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getExchangesList">
            <el-button slot="append" icon="el-icon-search" @click="getExchangesList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- table表格区域 -->
      <el-table :data="exchangeslist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="product.productName"></el-table-column>
        <el-table-column label="商品积分" prop="product.productPoint" ></el-table-column>
        <el-table-column label="用户名字" prop="user.username" ></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>

      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>

  export default {
    data() {
      return {
        // 查询参数对象
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 5
        },
        // 商品列表
        exchangeslist: [],
        // 总数据条数
        total: 0
      }
    },
    created() {


      this.queryInfo.query = window.sessionStorage.getItem('userId')
      this.getExchangesList()
      this.queryInfo.query = window.sessionStorage.getItem('userId')
      window.sessionStorage.setItem('userId', '')
    },
    methods: {
      // 根据分页获取对应的商品列表
      async getExchangesList() {
        const { data: res } = await this.$http.get('exchanges', {
          params: this.queryInfo
        })

        if (res.meta.status !== 200) {
          return this.$message.error('获取兑换列表失败！')
        }

        this.$message.success('获取兑换列表成功！')
        console.log(res.data)
        this.exchangeslist = res.data.data
        this.total = res.data.total
      },
      handleSizeChange(newSize) {
        this.queryInfo.pagesize = newSize
        this.getGoodsList()
      },
      handleCurrentChange(newPage) {
        this.queryInfo.pagenum = newPage
        this.getGoodsList()
      },

    }
  }
</script>

<style lang="less" scoped>
  .el-checkbox {
    margin: 0 10px 0 0 !important;
  }

  .previewImg {
    width: 100%;
  }

  .btnAdd {
    margin-top: 15px;
  }
</style>
